# import requests
# #1.获取单张图片
# url = 'https://p1.music.126.net/meKwMrqLrzQqJj7PkCLL4w==/109951169798001388.jpg?imageView&quality=89'
#
# headers={
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
# }
#
# #发送请求,获取响应
# res = requests.get(url,headers=headers)
#
# with open('网易云.jpg','wb') as f:
#     f.write(res.content)

#2.获得单首歌曲
import requests

# url = 'https://cn-hncs-cu-01-07.bilivideo.com/upgcxcode/31/77/1604757731/1604757731_x1-1-100026.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1721446762&gen=playurlv2&os=bcache&oi=0&trid=000014ca5c72873d4ea98b4a5abb6a6efad1p&mid=3494368394611488&platform=pc&og=hw&upsig=b04b4e4bf1aae2a206810a563f308d06&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform,og&cdnid=3296&bvc=vod&nettype=0&orderid=0,3&buvid=4B910931-FD59-CC19-90EF-96035E615E7062249infoc&build=0&f=p_0_0&agrr=1&bw=50483&logo=80000000'
# res2 = requests.get(url)
#
# with open('网易云.mp3','wb') as f:
#     f.write(res2.content)

#3.获得单个mv
# url ='https://vodkgeyttp8.vod.126.net/cloudmusic/2a16/core/2c66/3313a716b7833d620ad88bcb5848b02e.mp4?wsSecret=ac3dcce23f133432f8698781356935c5&wsTime=1721440321'
#
# res3 = requests.get(url)
#
# with open('网易云.mp4','wb') as f:
#     f.write(res3.content)

#4.贴吧单页获取
# url = 'https://tieba.baidu.com/hottopic/browse/hottopic?topic_id=27250145&topic_name=%E9%BB%84%E5%AE%A3%E6%B7%98%E6%B1%B0'
#
# headers={
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
# }
#
# res = requests.get(url,headers=headers)
#
# with open('黄宣.html','wb')as f:
#     f.write(res.content)

#第一页    https://tieba.baidu.com/f?kw=%E9%AC%BC%E7%81%AD&ie=utf-8&pn=0
#第二页    https://tieba.baidu.com/f?kw=%E9%AC%BC%E7%81%AD&ie=utf-8&pn=50
#第三页    https://tieba.baidu.com/f?kw=%E9%AC%BC%E7%81%AD&ie=utf-8&pn=100
#第四页    https://tieba.baidu.com/f?kw=%E9%AC%BC%E7%81%AD&ie=utf-8&pn=150

#规律
#pn:第一页0 第二页50 第三页100 第四页150

# url='https://tieba.baidu.com/f?'
#
# headers={
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
# }
#
# word = input('请输入贴吧名字:')
# page = int(input('请输入要保存的页数')) #int转变为整型
#
# #params参数的作用就是将参数值对进行拼接，构成完整的url地址
# for i in range(page):
#     params={
#         'kw':word,  #关键字
#         'pn':i*50   #页数
#     }
#     #发送请求
#     response = requests.get(url,headers=headers,params=params)
#
#     #保存数据
#     with open(f'{word}{i+1}.html','wb')as f:#f''格式化 {} 表示被替换字段，其中直接填入替换内容
#         f.write(response.content)



#6.改写为面向对象

class Tieba:
    def __init__(self):
        self.url='https://tieba.baidu.com/f?'
        self.headers={
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
        }

    #发送请求
    def send(self,params):
        res=requests.get(self.url,headers=self.headers,params=params)
        return res.text

    def save(self,page,con):
        with open(f'{page}.html','w',encoding='utf-8')as f:
            f.write(con)#往con里面写入数据

    def run(self):
        word = input('请输入贴吧名字:')
        pages = int(input('请输入要保存的页数')) #int转变为整型

        for page in range(pages):
            params={
                'kw':word,
                'pn':page*50
            }

        data = self.send(params)
        self.save(page,data)

te = Tieba()
te.run()